package br.com.casablanca.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.casablanca.dto.LoginDTO;
import br.com.casablanca.utils.HibernateUtility;

public class LoginDAO {
	public LoginDTO consultar(String usuario, String senha) throws Exception{
		LoginDTO login = null;
		Session sessao = HibernateUtility.getSession();
		Transaction tx = null;
		try {
			tx = sessao.beginTransaction();
			Query query = sessao.createQuery("from LoginDTO cli where cli.usuario =:usuario and cli.senha =:senha ");		
			query.setString("usuario", usuario);
			query.setString("senha", senha);
			login = (LoginDTO) query.uniqueResult();			
			tx.commit();			
		} catch (Exception e) {			
			tx.rollback();
			throw e;
		} finally {
			sessao.close();
		}
		return login;		
	}
	
	public void atualizaSkin(String skin, LoginDTO login){
		Session sessao = HibernateUtility.getSession();
		Transaction tx = null;
		try {
			tx = sessao.beginTransaction();
			LoginDTO temp = (LoginDTO) sessao.get(LoginDTO.class, login.getId());
			temp.setSkin(skin);
			sessao.update(temp);
			tx.commit();			
		} catch (Exception e) {			
			tx.rollback();
		} finally {
			sessao.close();
		}
	}
	
}
